package top.hmtools.mapperUnit;

import org.springframework.stereotype.Component;

import top.hmtools.base.StringTools;
import top.hmtools.beans.ColumnsInfoBean;
import top.hmtools.common.ECurdType;
import top.hmtools.common.TableNameTools;
import top.hmtools.system.SystemInfoTools;

/**
 * 获取批量添加的 mapper.xml sql语句片段
 * @author Pucou
 *
 */
@Component
public class AddSomeMapperUnit extends AMapperUnitBase{

	public ECurdType getCurdType() {
		return ECurdType.CREAT;
	}

	public String createContent(String basePackage, String methodName,ColumnsInfoBean... beans) {
		StringBuffer result = new StringBuffer();
		String tableAliseName = "aa";
		String tableName = beans[0].getTable_name();
        if(StringTools.isBlank(methodName)){
            methodName = "addSome";
        }
		
		result.append("<insert id=\""+methodName+"\" parameterType=\""+TableNameTools.getBeanName(basePackage, tableName)+"\">");
		result.append(SystemInfoTools.getOSLineSeparator());
		
		result.append("INSERT INTO "+tableName);
		result.append(SystemInfoTools.getOSLineSeparator());
		result.append("(");
		result.append(SystemInfoTools.getOSLineSeparator());
		for(int ii=0;ii<beans.length;ii++){
			ColumnsInfoBean bean = beans[ii];
			
			result.append(bean.getColumn_name());
			if(ii<beans.length-1){
				result.append(",");
			}
			result.append(SystemInfoTools.getOSLineSeparator());
		}
		result.append(")");
		
		result.append(SystemInfoTools.getOSLineSeparator());
		result.append("VALUES");
		result.append(SystemInfoTools.getOSLineSeparator());
		result.append("<foreach collection=\"beans\" item=\"item\" index=\"index\" separator=\",\" >");
		result.append(SystemInfoTools.getOSLineSeparator());
		result.append("(");
		result.append(SystemInfoTools.getOSLineSeparator());
		for(int ii=0;ii<beans.length;ii++){
			ColumnsInfoBean bean = beans[ii];
			
			if(this.gmConfiguration.isUseCamelName()){
				result.append("#{item."+TableNameTools.underlineToCamelByRegex(bean.getColumn_name())+"}");
			}else{
				result.append("#{item."+bean.getColumn_name()+"}");
			}
			if(ii<beans.length-1){
				result.append(",");
			}
			result.append(SystemInfoTools.getOSLineSeparator());
			result.append("");
		}
		result.append(")");
		result.append(SystemInfoTools.getOSLineSeparator());
		result.append("</foreach>");
		result.append("");
		result.append(SystemInfoTools.getOSLineSeparator());
		result.append("</insert>");
		
		return result.toString();
	}

}
